package test;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Que {
    static int N = 100001;
    static int[] a =new int[N];
    static Queue<Integer>[]  q = new LinkedList[N];
    static int[][] dp = new int[N][2];
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int n = scan.nextInt();
        for (int i = 1; i <=n; i++) {
            a[i] = scan.nextInt();
            q[i] = new LinkedList<>();
        }
        boolean[] flag = new boolean[N+1];
        for (int i = 1; i < n; i++) {
            int u = scan.nextInt();
            int v = scan.nextInt();
            q[v].add(u);
            flag[u] = true;
        }
        for(int i = 1;i<=n;i++){
            if(!flag[i]){
                dsf(i);
                System.out.println(Math.max(dp[i][0],dp[i][1]));
                break;
            }
        }
        scan.close();
    }
    public static void dsf(int i){
        for(int u : q[i]){
            dsf(u);
            dp[i][0] += Math.max(dp[u][0],dp[u][1]);
            dp[i][1] += dp[u][0];
        }
        dp[i][1]+=a[i];
    }
}
